package com.zhou.project.components.base.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @project spring-boot-project
 * @author zhouhonggang
 * @datetime 2021/8/14 16:53
 * @version 1.0.0
 * @description: 响应对象
 */
@Getter
@Setter
public class ResultData {
    private Integer code;
    private String message;
    private Object data;

    public ResultData() {}

    public ResultData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 成功 | 无参
     * @return ResultData
     */
    public static ResultData success() {
        ResultData result = new ResultData();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    /**
     * 成功 | 带参
     * @return ResultData
     */
    public static ResultData success(Object data) {
        ResultData result = new ResultData();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    /**
     * 自定义成功 | 带参
     * @return ResultData
     */
    public static ResultData success(ResultCode resultCode) {
        ResultData result = new ResultData();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 自定义成功 | 带参
     * @return ResultData
     */
    public static ResultData success(ResultCode resultCode, Object data) {
        ResultData result = new ResultData();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    /**
     * 错误信息 | 无参
     * @return ResultData
     */
    public static ResultData failure() {
        ResultData result = new ResultData();
        result.setResultCode(ResultCode.FAILURE);
        return result;
    }

    /**
     * 错误信息 | 无参
     * @return ResultData
     */
    public static ResultData failure(ResultCode resultCode) {
        ResultData result = new ResultData();
        result.setResultCode(resultCode);
        return result;
    }

    /**
     * 错误信息 | 带参
     * @return ResultData
     */
    public static ResultData failure(ResultCode resultCode, Object data) {
        ResultData result = new ResultData();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public void setResultCode(ResultCode code) {
        this.code = code.code();
        this.message = code.message();
    }

    /**
     * 封装响应状态信息
     * @param response 响应
     * @param message 消息
     */
    public static void response(HttpServletResponse response, ResultData message)
    {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        String json = null;
        try {
            json = objectMapper.writeValueAsString(message);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        response.setContentType("application/json;charset=utf-8");
        //response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.write(json);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

}
